#region Using

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;

using Gizmox.WebGUI.Common;
using Gizmox.WebGUI.Forms;
using VisualDB.DAL;
using VisualDB.Entities;
using VisualDB.Helpers;

#endregion

namespace VisualDB
{
    public partial class AddEditTableUC : UserControl
    {
        private IDatabaseDAL _databaseDAL;
        private TableEntity _table;
        private AddEditMode _addEditMode;
        private List<ColumnEntity> _originalColumns;

        public AddEditTableUC(IDatabaseDAL databaseDAL, TableEntity table, AddEditMode addEditMode)
        {
            InitializeComponent();
            _databaseDAL = databaseDAL;
            _table = table;
            _addEditMode = addEditMode;
        }

        private void AddEditTableUC_Load(object sender, EventArgs e)
        {
            this.grdColumns.AutoGenerateColumns = false;

            if (_addEditMode == AddEditMode.Edit)
            {
                _originalColumns = _databaseDAL.GetColumns(_table);

                DataTable table = UtilityHelper.GetDataTableFromArray(_originalColumns.ToArray());

                this.grdColumns.DataSource = table;
            }
            else
            {
                _originalColumns = new List<ColumnEntity>();
            }
        }

        private void toolBar_ButtonClick(object sender, ToolBarButtonClickEventArgs e)
        {
            switch (e.Button.Tag.ToString())
            {
                case "DeleteColumn":
                    List<DataGridViewRow> rows = new List<DataGridViewRow>();

                    foreach (DataGridViewCell cell in this.grdColumns.SelectedCells)
                    {
                        DataGridViewRow row = this.grdColumns.Rows[cell.RowIndex];

                        if (!rows.Contains(row))
                            rows.Add(row);
                    }

                    foreach (DataGridViewRow row in rows)
                    {
                        this.grdColumns.Rows.Remove(row);
                    }

                    break;

                case "SaveTable":
                    if (_addEditMode == AddEditMode.Add)
                    {
                        RenameForm rename = new RenameForm("Save Table", RenameType.Table, _table.Name);
                        rename.Closed += new EventHandler(rename_Closed);
                        rename.ShowDialog(this.Form);
                    }
                    else
                    {
                        SaveTable();
                    }

                    break;
            }
        }

        private void SaveTable()
        {
            bool newTable = (_addEditMode == AddEditMode.Add);

            List<ColumnEntity> currentColumns = this.GetCurrentColumns();
            string query = _databaseDAL.SaveTable(newTable, _table, _originalColumns, currentColumns);
            PresentationLayer.Instance.ExecuteQuery(_databaseDAL, query);
        }

        void rename_Closed(object sender, EventArgs e)
        {
            RenameForm rename = (RenameForm)sender;

            if (rename.DialogResult == DialogResult.OK)
            {
                _table.Name = rename.NewName;
                SaveTable();
            }
        }

        private List<ColumnEntity> GetCurrentColumns()
        {
            List<ColumnEntity> columns = new List<ColumnEntity>();

            foreach (DataGridViewRow row in this.grdColumns.Rows)
            {
                if (row.Cells["ColumnName"].Value == null)
                    break;

                ColumnEntity column = new ColumnEntity();
                column.Id = string.IsNullOrEmpty(row.Cells["Id"].Value as string) ? System.Guid.NewGuid().ToString() : Convert.ToString(row.Cells["Id"].Value);
                column.Name = Convert.ToString(row.Cells["ColumnName"].Value);
                column.Type = Convert.ToString(row.Cells["ColumnType"].Value);
                column.AllowNulls = UtilityHelper.ConvertToBool(row.Cells["AllowNulls"].Value);
                column.PrimaryKey = UtilityHelper.ConvertToBool(row.Cells["PrimaryKey"].Value);

                columns.Add(column);
            }

            return columns;
        }
    }
}